<template>
  <div class="list">
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="$router.push({path:'/menu/add'})">添加</el-button>
      </el-col>
    </el-row>
    <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;margin-top: 10px;" row-key="id" border default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <!-- <el-table-column prop="id" label="ID">
      </el-table-column> -->
      <el-table-column prop="menu_name" label="菜单名称">
      </el-table-column>
      <el-table-column prop="route" label="路由">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="toEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="toDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>


</template>

<script>
  import {
    ajaxPost
  } from "@/api/ajax"
  export default {
    name: "menuList",
    data() {
      return {
        tableData: [],
        page: 1,
        size: 15
      };
    },
    created() {
      this.getMenuList();
    },
    methods: {
      getMenuList() {
        let data = {
          page: this.page,
          size: this.size,
        };
        ajaxPost('menu/list', data).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list;
          }
        })
      },
      toEdit(row) {
        this.$router.push({ 
          name: 'menu_edit',
          params: row
        });
      },
      toDel(id) {
        this.$confirm('确认删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          ajaxPost('menu/delete',{id:id}).then(res=>{
            if(res.code == 0){
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
            }else{
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            }
          })
          this.getMenuList();
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },
  }
</script>

<style lang="scss">
  .list {
    padding: 20px;
    box-sizing: border-box;

    .search-input {
      width: 150px;
    }

    .list-page {
      margin-top: 20px;
    }
  }
</style>
